import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'

dayjs.locale('zh-cn')

// 格式化日期时间
export const formatDateTime = (timestamp: number): string => {
  return dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss')
}

// 格式化日期
export const formatDate = (timestamp: number): string => {
  return dayjs(timestamp).format('YYYY-MM-DD')
}

// 格式化相对时间
export const formatRelativeTime = (timestamp: number): string => {
  const now = dayjs()
  const target = dayjs(timestamp)
  const diffHours = now.diff(target, 'hour')
  
  if (diffHours < 1) {
    const diffMinutes = now.diff(target, 'minute')
    return `${diffMinutes}分钟前`
  } else if (diffHours < 24) {
    return `${diffHours}小时前`
  } else {
    const diffDays = now.diff(target, 'day')
    if (diffDays < 7) {
      return `${diffDays}天前`
    } else {
      return formatDate(timestamp)
    }
  }
}

// 格式化温度
export const formatTemperature = (temp: number, unit: 'C' | 'F' = 'C'): string => {
  if (unit === 'F') {
    return `${Math.round(temp * 9 / 5 + 32)}°F`
  }
  return `${temp}°C`
}

// 格式化百分比
export const formatPercent = (value: number): string => {
  return `${value}%`
}

// 格式化风速
export const formatWindSpeed = (speed: number): string => {
  return `${speed} km/h`
} 